package com.aistar.service.impl;

import com.aistar.mapper.ProductReviewImagesMapper;
import com.aistar.mapper.ProductReviewMapper;
import com.aistar.pojo.ProductExample;
import com.aistar.pojo.ProductReview;
import com.aistar.pojo.ProductReviewExample;
import com.aistar.pojo.ProductReviewImages;
import com.aistar.pojo.pojoOV.Review;
import com.aistar.service.ProductReviewImagesService;
import com.aistar.service.ProductReviewService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductReviewServiceImpl implements ProductReviewService {



    @Autowired
    private ProductReviewMapper productReviewMapper;
    @Autowired
    private ProductReviewImagesService productReviewImagesService;

    @Override
    public ServerResponse add(ProductReview productReview) {
        return null;
    }

    @Override
    public ServerResponse modify(ProductReview productReview) {
        return null;
    }

    @Override
    public ServerResponse delete(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
           return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    @Override
    public ServerResponse getByPage(Integer page) {
        ProductReviewExample productReviewExample = new ProductReviewExample();
        productReviewExample.createCriteria().andStatusEqualTo("1");
        Page<ProductReview> productReviewPage = PageHelper.startPage(page, 100);
        List<ProductReview> productReviewList = productReviewMapper.selectByExample(productReviewExample);
        PageInfo pageInfo = productReviewPage.toPageInfo();
        // System.out.println(pageInfo);
        List list = new ArrayList<>();
        List<ProductReview> productReviewArrayList = new ArrayList<>();
        for (ProductReview productReview:productReviewList) {
            Integer id = productReview.getId();
           ServerResponse serverResponse = productReviewImagesService.getByProductReviewId(id);
           if(serverResponse.getStatus() == 1){
               List<ProductReviewImages> productReviewImages = (List<ProductReviewImages>) serverResponse.getData();
               Review review = new Review(productReview,productReviewImages);
               list.add(review);
           }else {
               Review review = new Review(productReview,null);
               list.add(review);
           }
        }
        pageInfo.setList(list);
       

        if (productReviewList != null && productReviewList.size() > 0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFailed("暂无评论");
    }

    @Override
    public ServerResponse addCountById(Integer id) {
        ProductReview productReview =  productReviewMapper.selectByPrimaryKey(id);
       Integer count =  productReview.getAgreeCount();
       count = count+1;
       productReview.setAgreeCount(count);
       int rows = productReviewMapper.updateByPrimaryKeySelective(productReview);
       if(rows>0)
           return ServerResponse.getSuccess(productReview);
        return ServerResponse.getFailed("点赞失败");
    }
    @Override
    public ServerResponse reduceCountById(Integer id) {
        ProductReview productReview =  productReviewMapper.selectByPrimaryKey(id);
        Integer count =  productReview.getAgreeCount();
        count = count-1;
        productReview.setAgreeCount(count);
        int rows = productReviewMapper.updateByPrimaryKeySelective(productReview);
        if(rows>0)
            return ServerResponse.getSuccess(productReview);
        return ServerResponse.getFailed("点赞失败");
    }
}
